package gjw.jimven.activity;

/**
 * @创建者 Jimven
 * @创建时间 2017/8/25/0025  10:16
 */

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import gjw.jimven.R;
import gjw.jimven.adapter.MyAdapter;
import gjw.jimven.base.BaseActivity;
import gjw.jimven.bean.DataBean;

/**
 * listview带有全选功能的CheckBox
 */
public class CheckBoxActivity extends BaseActivity implements View.OnClickListener {

    private Button         mBtn_checkbox1;
    private Button         mBtn_checkbox2;
    private Button         mBtn_checkbox3;
    private Button         mBtn_checkbox4;
    private Button         mBtn_checkbox5;
    private ListView       mLv_checkbox;
    private List<DataBean> mDatas;
    private MyAdapter      mMyAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_checkbox;
    }

    @Override
    protected void setUpView() {
        initView();
        initData();
        initListener();
        initEvent();

    }

    private void initView() {
        mBtn_checkbox1 = (Button) findViewById(R.id.btn_checkbox1);
        mBtn_checkbox2 = (Button) findViewById(R.id.btn_checkbox2);
        mBtn_checkbox3 = (Button) findViewById(R.id.btn_checkbox3);
        mBtn_checkbox4 = (Button) findViewById(R.id.btn_checkbox4);
        mBtn_checkbox5 = (Button) findViewById(R.id.btn_checkbox5);
        mLv_checkbox = (ListView) findViewById(R.id.lv_checkbox);
    }

    private void initData() {
        mDatas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            DataBean dataBean = new DataBean("" + i, "上邪", "山无棱，天地合，乃敢与君绝");
            mDatas.add(dataBean);
        }
        mMyAdapter = new MyAdapter(mContext, mDatas);
        mLv_checkbox.setAdapter(mMyAdapter);
    }

    private void initListener() {
        mBtn_checkbox1.setOnClickListener(this);
        mBtn_checkbox2.setOnClickListener(this);
        mBtn_checkbox3.setOnClickListener(this);
        mBtn_checkbox4.setOnClickListener(this);
        mBtn_checkbox5.setOnClickListener(this);
    }

    private void initEvent() {

    }


    @Override
    public void onClick(View view) {
        int Id = view.getId();
        switch (Id) {
            case R.id.btn_checkbox1:
                mMyAdapter.flage = !mMyAdapter.flage;
                if (mMyAdapter.flage) {
                    mBtn_checkbox1.setText("取消");
                } else {
                    mBtn_checkbox1.setText("编辑");
                }
                mMyAdapter.notifyDataSetChanged();
                break;

            case R.id.btn_checkbox2:
                if (mMyAdapter.flage) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        mDatas.get(i).isCheck = true;
                    }
                    mMyAdapter.notifyDataSetChanged();
                }
                break;

            case R.id.btn_checkbox3:
                if (mMyAdapter.flage) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        mDatas.get(i).isCheck = false;
                    }
                    mMyAdapter.notifyDataSetChanged();
                }
                break;

            case R.id.btn_checkbox4:
                if (mMyAdapter.flage) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        if (mDatas.get(i).isCheck) {
                            mDatas.get(i).isCheck = false;
                        } else {
                            mDatas.get(i).isCheck = true;
                        }
                    }
                    mMyAdapter.notifyDataSetChanged();
                }
                break;

            case R.id.btn_checkbox5:
                List<String> ids = new ArrayList<>();
                if (mMyAdapter.flage) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        if (mDatas.get(i).isCheck) {
                            ids.add(mDatas.get(i).id);
                        }
                    }
                    Toast.makeText(mContext, ids.toString(), Toast.LENGTH_SHORT).show();
                    Log.e("TAG", ids.toString());
                }

                break;
            default:
                break;

        }

    }
}
